<?php
namespace app\admin\controller;
use think\Db;
class Pool extends Base{
    public function money(){
        if(request()->isAjax ()){
            extract (input ());
            $map = [];
            if(isset($role)&&$role != ""){
                $map['o_pay_type'] = $role;
            }
            if(isset($status)&&$status != ""){
                $map['o_tixian_status'] = $status;
            }
            if(session ('area')){
                $map['user_area'] = ['in',session('area')];
            }
            if(isset($key)&&$key!="")
            {
                $map['o_order_num|o_account'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('m_order_tixian o')
                ->join('u_user u','o.o_user_id = u.user_id','left')
                ->where($map)
                ->count();
            $lists = Db::name('m_order_tixian o')
                ->join('u_user u','o.o_user_id = u.user_id','left')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('o_add_time desc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        $whereForShop = [
            'o_tixian_status' => 2
        ];
        if(session ('area')){
            $whereForShop['user_area'] = ['in',session('area')];
        }
        $shop = Db::name('m_order_tixian o')
            ->join('u_user u','o.o_user_id = u.user_id','left')
            ->where($whereForShop)
            ->sum('o_money');
        $month = Db::name('m_order_tixian o')
            ->join('u_user u','o.o_user_id = u.user_id','left')
            ->whereTime('o_add_time', 'last month')
            ->where($whereForShop)
            ->sum('o_money');
        $today =Db::name('m_order_tixian o')
            ->join('u_user u','o.o_user_id = u.user_id','left')
            ->whereTime('o_add_time', 'today')
            ->where($whereForShop)
            ->sum('o_money');
        $yesterday =  Db::name('m_order_tixian o')
            ->join('u_user u','o.o_user_id = u.user_id','left')
            ->whereTime('o_add_time', 'yesterday')
            ->where($whereForShop)
            ->sum('o_money');
        return $this->fetch('pool/money',['shop'=>$shop,'month'=>$month,'today'=>$today,'yesterday'=>$yesterday]);
    }


    public function tixian(){
        extract (input ());
        $order = Db::name('m_order_tixian')->where(['o_id'=>$o_id])->find();
        $conf = Db::name('jifen_conf')->where(['conf_id'=>30])->value ('conf_rate');//手续费比例
        if($order['o_tixian_status'] != 1){
            return json(['code'=>100,'msg'=>'只有待审核的才可以提现！']);
        }
        $money = $order['o_money'] - ($order['o_money'] * $conf);//最终金额
        Db::startTrans ();
        try{
            if($type == 2){//通过
                if($order['o_pay_type'] == 1){//支付宝
                    $ali = new \app\api\controller\Ali();
                    $res =  $ali->tixianAgreeShopOrWork($order['o_order_num'],$money,$order['o_account'],$order['o_real_name']);
                    $msg = '支付宝';
                }else if($order['o_pay_type'] == 2){//微信
                    if($order['o_openid'] == ''){
                        return json(['code'=>100,'msg'=>'提现失败，缺少openid']);
                    }
                    $wx = new \app\api\controller\Weixin();
                    $res = $wx->userTiXian($order['o_order_num'],$order['o_openid'],$money,$order['o_real_name']);
                    $msg = '微信';
                }else{//微信
                    if($order['o_openid'] == ''){
                        return json(['code'=>100,'msg'=>'提现失败，缺少openid']);
                    }
                    $wx = new \app\api\controller\Wechat();
                    $res = $wx->userTiXian($order['o_order_num'],$order['o_openid'],$money,$order['o_real_name']);
                    $msg = '微信';
                }
                if($res['status'] == 200){
                    Db::name('admin')->where('id',1)->setDec ('balance',$money);
                    Db::name('m_order_tixian')->where(['o_id'=>$o_id])->update (['o_third_order_num'=>$res['msg'],'o_tixian_status'=>$type,'o_dotime'=>date('Y-m-d H:i:s')]);
                    Db::name('notice')->insert (['n_to'=>1,'n_title'=>'产品池余额提现','n_content'=>'您提现了'.$order['o_money'].'元'."，已提现至您的{$msg}账户，请注意查收！",'n_images'=>'','n_type'=>3,'n_user_id'=>$order['o_user_id'],'n_read'=>1,'n_delete'=>1,'n_time'=>date('Y-m-d H:i:s')]);
                    $this->jpushUser(1,$order['o_user_id'],'您提现了'.$order['o_money'].'元'."，已提现至您的{$msg}账户，请注意查收！",json([]));//Todo

                }else{
                    return json(['code'=>100,'msg'=>$res['msg']]);
                }
            }else{
                //钱先加回去
                Db::name('u_user')->where('user_id',$order['o_user_id'])->setInc('user_m_pool_money',$order['o_money']);
                Db::name('m_order_tixian')->where(['o_id'=>$o_id])->update (['o_tixian_status'=>$type,'o_dotime'=>date('Y-m-d H:i:s')]);
                Db::name('notice')->insert (['n_to'=>1,'n_title'=>'产品池余额提现','n_content'=>'您的提现申请已被驳回','n_images'=>'','n_type'=>3,'n_user_id'=>$order['o_user_id'],'n_read'=>1,'n_delete'=>1,'n_time'=>date('Y-m-d H:i:s')]);
                $this->jpushUser(1,$order['o_user_id'],'您的提现申请已被驳回',json([]));//Todo
            }
            Db::commit ();
            return json(['code'=>200,'msg'=>'操作成功']);
        }catch (\Exception $e){
            Db::rollback ();
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
    public function index(){
        if(request()->isAjax ()){
            extract (input ());
            $map = [
                'record_from_type' => ['not in' , '1,2']
            ];
            if(isset($record_from_type)&&$record_from_type != ""){
                $map['record_from_type'] = $record_from_type;
            }
            if(session ('area')){
                $map['user_area'] = ['in',session('area')];
            }
            if(isset($key)&&$key!="")
            {
                $map['record_order_number'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('m_record o')
                ->join('u_user u','o.record_user_id = u.user_id','left')
                ->where($map)
                ->count();
            $lists = Db::name('m_record o')
                ->join('u_user u','o.record_user_id = u.user_id','left')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('record_add_time desc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        $whereForShop = [
            'record_id' => ['>',0]
        ];
        if(session ('area')){
            $whereForShop['user_area'] = ['in',session('area')];
        }
        $shop = Db::name('m_record o')
            ->join('u_user u','o.record_user_id = u.user_id','left')
            ->where($whereForShop)
            ->where('record_from_type','in','5')
            ->sum('record_money');
        $month = Db::name('m_record o')
            ->join('u_user u','o.record_user_id = u.user_id','left')
            ->where($whereForShop)
            ->where('record_from_type','in','6')
            ->sum('record_money');
        $today =Db::name('m_record o')
            ->join('u_user u','o.record_user_id = u.user_id','left')
            ->where($whereForShop)
            ->where('record_from_type','in','3,4')
            ->field('sum(record_money) record_money,sum(record_jifen) record_jifen')
            ->find();
        $yesterday =  Db::name('m_record o')
            ->join('u_user u','o.record_user_id = u.user_id','left')
            ->where($whereForShop)
            ->where('record_from_type','in','7,8')
            ->field('sum(record_money) record_money,sum(record_jifen) record_jifen')
            ->find();
        return $this->fetch('pool/index',['shop'=>$shop,'month'=>$month,'today'=>$today,'yesterday'=>$yesterday]);
    }
    public function morder(){
        if(request()->isAjax ()){
            extract (input ());
            $map = [
                'mo_pay_status' => 2
            ];
            if(isset($mo_pay_type)&&$mo_pay_type != ""){
                $map['mo_pay_type'] = $mo_pay_type;
            }
            if(isset($mo_status)&&$mo_status != ""){
                $map['mo_status'] = $mo_status;
            }
            if(isset($mo_store_uid)&&$mo_store_uid != ""){
                $map['mo_store_uid'] = $mo_store_uid;
            }
            if(session ('area')){
                $map['user_area'] = ['in',session('area')];
            }
            if(isset($key)&&$key!="")
            {
                $map['mo_order_num'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('m_order o')
                ->join('u_user u','o.mo_user_id = u.user_id','left')
                ->join('product p','o.mo_p_id = p.p_id','left')
                ->where($map)
                ->count();
            $lists = Db::name('m_order o')
                ->join('u_user u','o.mo_user_id = u.user_id','left')
                ->join('product p','o.mo_p_id = p.p_id','left')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('mo_time desc')
                ->field('o.*,user_id,user_username,p_name')
                ->select ();
            if($lists){
                foreach ($lists as $k => $v){
                    if($v['mo_store_uid'] > 0){
                        $lists[$k]['storeName'] = Db::name('u_user')
                            ->where('user_id',$v['mo_store_uid'])
                            ->value('user_username');
                    }else{
                        $lists[$k]['storeName'] = '产品池库购买';
                    }
                    if($v['mo_get_user_id'] > 0){
                        $lists[$k]['getName'] = Db::name('u_user')
                            ->where('user_id',$v['mo_get_user_id'])
                            ->value('user_username');
                    }else{
                        $lists[$k]['getName'] = '无';
                    }
                }
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        $users = Db::name('u_user')
            ->where('user_level','>',1)
            ->field('user_id,user_username')
            ->select ();
        return $this->fetch('pool/morder',['users'=>$users]);
    }
}